<template>
	<view class="hwq-content">
		<navBar title="选择项目"></navBar>
		<searchCom class="search-input" v-model="searchText" @enter="fnSearch" @change="fnSearch"></searchCom>
		<scroll-view scroll-y="true" class="hwq-scroll-box hwq-is-search">
			<template v-for="item1 in data">
				<!-- :key="item1.id+'-0'" -->
				<view class="item-box" :key="item1.id" @click="clickItem(item1.id,0)">
					<view>{{item1.name||""}}</view>
					<view class="ico-box">
						<u-icon :name="showItem(item1.id,0)?'arrow-down':'arrow-right'" color="#A7AEBD" size="28"></u-icon>
					</view>
				</view>
				<template v-for="item2 in item1.list" v-if="showItem(item1.id,0)">
					<view class="item-box item-box-area" :key="item2.id" @click="clickItem(item1.id+'-'+item2.id)">
						<view>{{item2.name||""}}</view>
						<view class="ico-box">
							<u-icon :name="showItem(item2.id,1)?'arrow-down':'arrow-right'" color="#A7AEBD" size="28"></u-icon>
						</view>
					</view>
					<template v-for="item3 in item2.list" v-if="showItem(item2.id,1)">
						<view class="item-box item-box-point" :key="item3.id" @click="clickPoint(item1,item2,item3)">
							<view>{{item3.name||""}}</view>
						</view>
					</template>
				</template>
			</template>
		</scroll-view>
	</view>
</template>

<script>
	import searchCom from "@/components/searchCom.vue"
	import mixins from '@/utils/mixins.js'
	import {
		getProjectUserList
	} from "@/api/baseManage/baseManage.js"
	import {
		getProjectOrgPrivilegeList
	} from "@/api/dataAuth/dataAuth.js"

	import {
		getTreeByNameByLike
	} from "@/api/emergency/polling.js"

	import myStroage from '@/utils/sessionStorage'

	export default {
		mixins: [mixins],
		data() {
			return {
				data: [],
				copyData:[],
				searchText: undefined,
				activeItem: "",
				selectedList:[]
			}
		},
		components: {
			searchCom
		},
		created() {
			this.getTreeByNameByLike()
			console.log(myStroage.getStorage('user_groupId'));
		},
		methods: {
			fnSearch(val) {
				this.data = this.mapTree(val,this.copyData)
			},
			searchChange(val) {
				if(!val){
					this.data = this.copyData
				}
			},
			//点击工点项
			clickPoint(item1,item2,item3){
				this.$$emit(this.$config.ENV_SELECT_PROJECT,[item1,item2,item3]);
				uni.navigateBack({})
			},

			showItem(id,index=0) {
				let arr = this.activeItem.split("-")
				return arr[index]==id
			},
			//点击菜单父选项
			clickItem(str) {
				this.activeItem = String(str)

			},

			// 获取项目列表
			getTreeByNameByLike() {
				getTreeByNameByLike({}).then(res => {
					console.log(res, '获取项目列表')
					this.tempList = res.result
					this.getList()
				})
			},
			getList() {
				console.log(this.$store.getters.userName.id);
				let query = {
					currPage: 1,
					pageSize: 100000,
					params: {
						userId: this.$store.getters.userName.id
						
					}
				}
				getProjectUserList(query).then(res => {
					this.projectId = myStroage.getStorage('user_groupId')
					this.tempList.forEach(item => {
						item.projectOrgId = res.result.list[0].projectOrgId
						item.projectId = this.projectId
					})
					this.projectOrgId = res.result.list[0].projectOrgId
					this.getProjectOrgPrivilegeList(res.result.list[0].projectOrgId)
				})
			},
			getProjectOrgPrivilegeList(projectOrgId) {
				let _query = {
					currPage: 1,
					pageSize: 100000,
					params: {
						projectOrgId: projectOrgId
					}
				}
				getProjectOrgPrivilegeList(_query).then(res => {
					this.selectPrivilege = res.result.list
					this.privilegeList = JSON.parse(JSON.stringify(this.tempList))
					let list = []
					let temp1 = []
					let temp2 = []
					let temp3 = []
					this.selectPrivilege.forEach(item => {
						temp1.push(item.bpId)
						temp2.push(item.sectionId)
						temp3.push(item.siteId)
					})
					list.push(temp1, temp2, temp3)

					this.privilegeList.forEach(value => {
						if (list[0].length !== 0) {
							if (list[0].includes(value.id)) {

								// 判断一级项目
								value.list.forEach(value2 => {
									if (list[1].includes(value2.id)) {
										// 判断二级标段
										value2.list.forEach(value3 => {
											if (list[2].includes(value3.id)) {

											} else {
												value2.list = value2.list.filter(item3 =>
													item3.id !== value3.id)
											}
										})
									} else {
										value.list = value.list.filter(item2 => item2.id !== value2
											.id)
									}
								})

							} else {
								this.privilegeList = this.privilegeList.filter(item => item.id !== value
									.id)
							}
						}
					})
					if (this.selectPrivilege.length === 0) {
						this.privilegeList = []
					}
					this.data = this.privilegeList
					this.copyData = this.privilegeList
					console.log(this.data, ' 绑定的值')
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.hwq-content {
		/* #ifdef MP-WEIXIN */
			padding-top: 200rpx;
		/* #endif */
	}
	.item-box {
		height: 120rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin: 0 30rpx;
		border-top: 1px solid #E2E5ED;

		&:first-child {
			border-top-width: 0;
		}

		.name {
			font-size: 36rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			color: #445469;
			line-height: 120rpx;
			border-bottom: 1px solid #E2E5ED;
		}
	}

	.item-box-area {
		padding-left: 30rpx;
	}

	.item-box-point {
		padding-left: 60rpx;
	}
</style>
